if( typeof Simple == "undefined") {
	var Simple = {};
}
Simple.mixin = $.extend;
Simple.log = function(msg) {
	if(window.console) {
		console.log(msg);
	}
}
Simple.namespace = function() {
	var a = arguments, o = this, i = 0, j, d, arg;
	for(; i < a.length; i++) {
		arg = a[i];
		if(arg.indexOf(".")) {
			d = arg.split(".");
			for( j = (d[0] == 'Simple') ? 1 : 0; j < d.length; j++) {
				o[d[j]] = o[d[j]] || {};
				o = o[d[j]];
			}
		} else {
			o[arg] = o[arg] || {};
		}
	}
	return o;
}
Simple.bridgeTojQuery = function(methodName, widget) {
	var methodArray = methodName.split(",");
	jQuery.each(methodArray, function(i, n) {
		jQuery.fn[n] = function(config) {
			return this.each(function() {
				if(config == "destroy") {
					if(jQuery.data(this, "widget_" + n)) {
						jQuery.data(this, "widget_" + n).destroy();
						$(this).removeData("widget_" + n);
						return;
					} else {
						return true;
					}
				}
				config = Simple.mixin({}, config, {
					node : this
				});
				//不要重复绑定
				if(jQuery.data(this, "widget_" + n)) {
					return;
				}
				var demo = new widget(config);
				jQuery.data(this, "widget_" + n, demo);
			});
		}
	});
}
//require,define
;
(function() {
	var require, define;
	Simple.loaded = {};
	Simple.defined = {};
	//队列
	var Queue = function(q) {
		this.queue = q || [];
	};
	Queue.prototype = {
		next : function() {
			return this.queue.shift();
		},
		last : function() {
			return this.queue.pop();
		},
		add : function() {
			this.queue.push.apply(this.queue, arguments);
		},
		size : function() {
			return this.queue.length;
		}
	};
	define = function(mName, callback) {
		//定义即加载
		if(!Simple.loaded[mName]) {

			Simple.loaded[mName] = true;
			if( typeof callback == "function") {
				Simple.defined[mName] = callback();
			} else {
				Simple.defined[mName] = callback;
			}
		}
	}
	var time, isLoad = false, loadJs = function(uri, callback,path) {
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = path?path+uri + ".js":uri+".js";
		document.getElementsByTagName("head")[0].appendChild(script);
		script.onreadystatechange = script.onload = function() {
			clearTimeout(time);
			Simple.loaded[uri] = true;
			callback();
			script.onreadystatechange = script.onload = null;
			isLoad = true;
		}
		time = setTimeout(function() {
			if(!isLoad) {
				Simple.log("装载资源失败：名称：" + uri + ",错误：装载超时！");
			}
		}, 5000);
	}
	var _loading = false,requireList,
	require = function(mName, callback,path) {
		//正在加载中。。将现在的函数放入队列
		if(!requireList) {
			requireList = new Queue();
			requireList.add({
				mName : mName,
				callback : callback
			});
		}
		if(_loading) {
			requireList.add({
				mName : mName,
				callback : callback
			});
		} else {
			//执行加载
			_dealLoadJs(mName, callback,path);
		}
	}
	var _dealLoadJs = function(mName, callback,path) {
		_loading = true;
		if( typeof mName == "string") {
			_dealLoadJsString(mName, callback,path)
		} else {
			_dealLoadJsArray(mName, callback,path);
		}
	}, _dealLoadJsString = function(mName, callback,path) {
		//加载当前模块
		if(isModuleExsit(mName)) {
			callback(getModuleObj(mName));
			requireList.next();
			_loading = false;
			if(requireList.queue[0]) {
				require(requireList.queue[0].mName, requireList.queue[0].callback);
			}
		} else {
			//从外部加载
			loadJs(mName, function() {
				callback(getModuleObj(mName));
				requireList.next();
				_loading = false;
				if(requireList.queue[0]) {
					require(requireList.queue[0].mName, requireList.queue[0].callback);
				}
			},path)
		}
	}, jsList, args = []; _dealLoadJsArray = function(mName, callback,path) {
		//将当前JS队列加载完毕
		jsList = jsList || new Queue(mName);
		//队列未加载完毕，继续加载直到加载完毕
		if(jsList.size() > 0) {
			if(isModuleExsit(jsList.queue[0])) {
				args.push(getModuleObj(jsList.queue[0]));
				jsList.next();
				_dealLoadJsArray(mName, callback);
			} else {
				loadJs(jsList.queue[0], function() {
					args.push(getModuleObj(jsList.queue[0]));
					jsList.next();
					_dealLoadJsArray(mName, callback);
				},path);
			}
			//队列加载完毕
		} else {
			callback.apply(this, args);
			requireList.next();
			_loading = false;
			requireList.queue[0] && require(requireList.queue[0].mName, requireList.queue[0].callback);
		}
	}, isModuleExsit = function(moduleName) {
		if(Simple.loaded[moduleName]) {
			return true;
		} else {
			return false;
		}
	}, getModuleObj = function(moduleName) {
		return Simple.defined[moduleName];
	}
	Simple.require=require;
	Simple.define=define;
})();
//declare;